//
//  ImgBrowserViewController.swift
//  Status
//
//  Created by 花轮 on 15/5/21.
//  Copyright (c) 2015年 花轮. All rights reserved.
//

import UIKit

private  let ID = "photoCell"

class ImgBrowserViewController: UIViewController, UICollectionViewDataSource {

    var imageURLs: [NSURL]?
    
    lazy var layout: UICollectionViewFlowLayout = {
        let l = UICollectionViewFlowLayout()
        
        l.itemSize = self.view.bounds.size
        l.scrollDirection = UICollectionViewScrollDirection.Horizontal
        l.minimumInteritemSpacing = 0
        // 水平间距
        l.minimumLineSpacing = 0
        
        
        return l
    }()
    
    // 用collectionview显示图片
    lazy var collectionView: UICollectionView = {
        let cv = UICollectionView(frame: self.view.bounds, collectionViewLayout: self.layout)
        cv.dataSource = self
        cv.pagingEnabled = true
        return cv
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.bounces = false
        collectionView.registerClass(SingleImgCell.self, forCellWithReuseIdentifier: ID)
    }
    
    override func loadView() {
        view = UIView(frame: UIScreen.mainScreen().bounds)
        view.addSubview(collectionView)
        
        let closeBtn = creatBtn("关闭")
        closeBtn.addTarget(self, action: "close", forControlEvents: UIControlEvents.TouchUpInside)
        let saveBtn = creatBtn("保存")
        
        var cons = [AnyObject]()
        
        cons += NSLayoutConstraint.constraintsWithVisualFormat("H:|-20-[closeBtn(80)]-20-[saveBtn(80)]", options: NSLayoutFormatOptions(0), metrics: nil, views: ["closeBtn": closeBtn, "saveBtn": saveBtn])
        cons += NSLayoutConstraint.constraintsWithVisualFormat("V:[closeBtn]-20-|", options: NSLayoutFormatOptions(0), metrics: nil, views: ["closeBtn": closeBtn])
        
        cons.append(NSLayoutConstraint(item: saveBtn, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: closeBtn, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        view.addConstraints(cons)
    }
    
    func close()
    {
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    func creatBtn(title: String) -> UIButton {
        let btn = UIButton()
        btn.setTitle(title, forState: UIControlState.Normal)
        btn.backgroundColor = UIColor.orangeColor()
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.layer.cornerRadius = 5
        btn.layer.masksToBounds = true
        btn.setTranslatesAutoresizingMaskIntoConstraints(false)
        view.addSubview(btn)
        
        return btn
    }
    
    
    // MARK: - collectionView
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageURLs?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ID, forIndexPath: indexPath) as! SingleImgCell
        
        cell.backgroundColor = UIColor(red: rndC(), green: rndC(), blue: rndC(), alpha: 1.0)
        
        cell.imageURL = imageURLs![indexPath.item]
        
//        println(self.childViewControllers)
        if !(childViewControllers as NSArray).containsObject(cell.singleVc!){
            addChildViewController(cell.singleVc!)
        }
        return cell
    }
    
    func rndC() -> CGFloat{
        return CGFloat(arc4random_uniform(256))/255
    }
}


class SingleImgCell: UICollectionViewCell {
    
    var singleVc: SingleImgViewController?
    
    var imageURL: NSURL? {
        didSet {
            singleVc?.imageURL = imageURL
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        singleVc = SingleImgViewController()
        singleVc?.view.frame = bounds
        
        addSubview(singleVc!.view)
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}
